﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YmtAuth.Domain.Model.Safe
{
    /// <summary>
    /// 用户设备验证
    /// </summary>
    public class UserDeviceAuthEntity
    {      
        public int UserId { get; private set; }
        public int AuthStatus { get; private set; }
        public DateTime? AuthTime { get; private set; }
        public string DeviceId { get; private set; }      
        public DateTime CreateTime { get; private set; }
        public string AppSource { get; private set; }
        public string Description { get; private set; }
        public DateTime? LastUpdateTime { get; private set; }
        public byte Type { get; private set; }
        /// <summary>
        /// client id 1
        /// </summary>
        public static byte ClientIdType = 1;
        /// <summary>
        /// device id 0
        /// </summary>
        public static byte DeviceIdType = 0;
        public UserDeviceAuthEntity(int userId, string deviceId,string addSource = "auth", string desc = null,byte type=0)
        {
            this.UserId = userId;
            this.DeviceId = deviceId;          
            this.AppSource = addSource;
            this.AuthStatus = 0;
            this.CreateTime=DateTime.Now;
            this.Description = desc;
            this.LastUpdateTime = DateTime.Now;
            this.Type = type;      
        }

        public void AuthIp(int authStatus)
        {
            this.AuthTime=DateTime.Now;
            this.AuthStatus = authStatus;
        }

        public bool IsDeviceId()
        {
            return this.Type == 0;
        }

        public bool IsClientId()
        {
            return this.Type == 1;
        }

        public void SetDeviceAuthInitStatus()
        {
            this.AuthTime = DateTime.Now;
            this.LastUpdateTime = DateTime.Now;
            this.AuthStatus = 0;
        }

        public void SetDeviceAuthStatus(bool clientVerifySuccess)
        {
            this.AuthTime=DateTime.Now;
            this.LastUpdateTime=DateTime.Now;
            this.AuthStatus = clientVerifySuccess ? 200 : -200;
        }

        public void SetDesc(string desc)
        {
            this.Description = desc;
        }

        public void SetType(byte type)
        {
            this.Type = type;
        }

        public void FlushLastUpdateTime()
        {
            this.LastUpdateTime=DateTime.Now;          
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="authStatus">200:success</param>
        public void AuthDeviceId(int authStatus=200)
        {
            this.AuthTime = DateTime.Now;
            this.AuthStatus = authStatus;
            this.LastUpdateTime = DateTime.Now;
        }
        public bool AuthIsSuccess()
        {
            return this.AuthStatus == 200;
        }      
        protected UserDeviceAuthEntity()
        {
        }
    }
}
